package com.example.ll_video.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.example.ll_video.bean.ResultData;
import com.example.ll_video.bean.UserInfo;
import com.example.ll_video.constant.Constant;
import com.example.ll_video.utils.AESUtil;
import com.example.ll_video.utils.DevicesUtil;
import com.example.ll_video.utils.HttpUtils;
import com.example.ll_video.utils.JsonUtil;
import com.example.ll_video.utils.TelephoneUtil;
import com.example.ll_video.utils.ToastUtils;
import com.qmuiteam.qmui.util.QMUIDeviceHelper;
import com.example.ll_video.R;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton;
import com.xwray.passwordview.PasswordView;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class RegisterActivity extends BaseActivity {
    @BindView(R.id.et_account)
    EditText etAccount;
    @BindView(R.id.et_password)
    PasswordView etPassword;
    @BindView(R.id.et_password_again)
    PasswordView etPasswordAgain;
    @BindView(R.id.register_btn)
    TextView registerBtn;

    @Override
    public void onCreate() {
        contentView(R.layout.register_activity_pp);
        ButterKnife.bind(this);
        setTitle("注册");
        showBackImage(view -> finish());

        registerBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                registerBtn.setEnabled(false);
                register();
            }
        });
    }

    private void register() {
        String devicesId = DevicesUtil.getSerialNumber(context);
        String source = Constant.SOURCE;

        String userName = etAccount.getText().toString().trim();
        if (TextUtils.isEmpty(userName)) {
            ToastUtils.showTextToast(context, "请输入电话号码");
            return;
        } else {
            TelephoneUtil.checkCellphone(userName);
            if (!TelephoneUtil.checkCellphone(userName)) {
                ToastUtils.showTextToast(context, "请输入正确的电话号码");
                return;
            }
        }

        String passwords = etPassword.getText().toString().trim();
        String passwordsAgain = etPasswordAgain.getText().toString().trim();
        if (TextUtils.isEmpty(passwords)) {
            ToastUtils.showTextToast(context, "请输入密码");
            return;
        } else {
            if (!passwords.equals(passwordsAgain)) {
                ToastUtils.showTextToast(context, "两次密码输入不一致");
                return;
            }
        }


        StringBuilder builder = new StringBuilder();
        builder.append("register?username=").append(userName);
        builder.append("|").append("password=").append(passwords);
        builder.append("|").append("devicesId=").append(devicesId);
        builder.append("|").append("source=").append(source);
        builder.append("|").append("client=").append("andr");
        String url = Constant.REGISTER + "?" + AESUtil.encrypt(builder.toString());

        HttpUtils.get(url, new HttpUtils.HttpCallBack() {
            @Override
            public void onSuccess(ResultData resultData) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        registerBtn.setEnabled(true);
                        if (!resultData.getMsg().equals("err")) {
                            UserInfo userInfo = JsonUtil.jsonToObject(resultData.getData(), UserInfo.class);
                            Intent intent = new Intent(context, LoginActivity.class);
                            intent.putExtra("userName", userInfo.getUsername());
                            startActivity(intent);
                            ToastUtils.showTextToast(context, "注册成功");
                        } else {
                            if (TextUtils.isEmpty(resultData.getMsg()))
                                ToastUtils.showTextToast(context, resultData.getMsg());
                            else ToastUtils.showTextToast(context, "注册失败");
                        }

                    }
                });
            }

            @Override
            public void onFail(String error) {

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        registerBtn.setEnabled(true);

                        ToastUtils.showTextToast(context, "注册失败，请重试");
                    }
                });
            }
        });
    }


}
